<template>
	<div class="left-box">
		<div class="heade-wrapper">
			<a-input-search
				:class="{ 'large-input-wraper': !showGroupSearch,'small-input-wraper': showGroupSearch, 'last-input-box': !showGroupSearch }"
				:placeholder="placeText"
				:disabled="radioValue === 'memberAttribute'"
				v-model.trim="searchValue"
				@search="onSearch"
			/>
			<a-checkbox
				@change="onSearchGroup"
				v-model="searchGroup"
				v-if="showGroupSearch"
				class="checkbox-wrapper"
			>
				<span
					class="label-text"
					:title="$i18n('selectPeople.search.group.label')"
				>
					{{ $i18n('selectPeople.search.group.label') }}
				</span>
			</a-checkbox>
		</div>

		<top-bottom-layout
			class="content-wrapper"
			ref="topBottomLayout"
			:isShowDown="selectOnlyDep"
		>
			<template #top>
				<transfer-tree
					:checkable="checkable"
					:checkedList="checkedList"
					:defaultExpandedKeys="defaultExpandedKeys"
					:defaultSelected="defaultSelected"
					:treeData="treeData"
					@toCheckedNode="onCheckedDeptNode"
					@toSelectNode="onSelectNode"
					@toScrollIntoSelectedNode="onScrollIntoSelectedNode"
				></transfer-tree>
			</template>
			<template #middle>
				<a-radio-group v-model="radioValue" @change="onRadioChange">
					<template v-for="(item, index) in radioList">
						<a-radio
							v-if="item.value === 'member' && selectType.includes('Member')"
							:key="index"
							:value="item.value"
							class="radio-box"
						>
							{{ item.label }}
						</a-radio>
						<a-radio
							v-if="!searchGroup && item.value === 'post' && !radioControl.hiddenPostOfDepartment"
							:key="index"
							:value="item.value"
							class="radio-box"
						>
							{{ item.label }}
						</a-radio>
						<a-radio
							v-if="!searchGroup && item.value === 'role' && !radioControl.hiddenRoleOfDepartment"
							:key="index"
							:value="item.value"
							class="radio-box"
						>
							{{ item.label }}
						</a-radio>
						<template
							v-if="!searchGroup && item.value === 'memberAttribute' && !radioControl.hiddenMetadataOfDepartment && metadataList.length > 0"
						>
							<a-radio
								class="radio-box"
								:key="index"
								:value="item.value"
							>
								<span></span>
							</a-radio>
							<a-select
								v-model="selectValue"
								@change="onMetaChange"
								:disabled="radioValue!=='memberAttribute'"
								size="small"
								notFoundContent="暂无数据"
								defaultActiveFirstOption
								:key="index + 'mateSelect'"
								class="small-select-option"
							>
								<a-select-option
									v-for="(item, index) in metadataList"
									:value="item.id"
									:key="index"
								>
									{{ item.name }}
								</a-select-option>
							</a-select>
						</template>
					</template>
				</a-radio-group>
			</template>
			<template #bottom>
				<scroll-list
					v-if="radioValue === 'member'"
					panel="Department"
					:command="memberCommond"
					ref="scrollList"
					key="member"
					:firstLoadSearch="false"
					:showOtherProp="true"
					:otherProps="memberOtherProps"
					:listDataHandler="listDataHandler"
					:departmentHandler="onChooseDeptNode"
				>
				</scroll-list>
				<scroll-list
					v-else-if="!radioControl.hiddenPostOfDepartment && radioValue === 'post' "
					panel="Department"
					command="postList"
					ref="scrollList"
					key="post"
					:firstLoadSearch="false"
					:showOtherProp="true"
					:otherProps="postOtherProps"
					:listDataHandler="listDataHandler"
				>
				</scroll-list>
				<scroll-list
					v-else-if="!radioControl.hiddenRoleOfDepartment && radioValue === 'role'"
					panel="Department"
					command="roleList"
					ref="scrollList"
					key="role"
					:firstLoadSearch="false"
					:showOtherProp="true"
					:otherProps="roleOtherProps"
				>
				</scroll-list>
				<scroll-list
					v-else-if="!radioControl.hiddenMetadataOfDepartment && radioValue === 'memberAttribute'"
					panel="Department"
					command="memberMetadataTagData"
					ref="scrollList"
					key="memberAttribute"
					:firstLoadSearch="false"
					:showOtherProp="false"
				>
				</scroll-list>
			</template>
		</top-bottom-layout>
	</div>
</template>

<script>
import transferTree from '@/components/transferTree.vue';
import scrollList from '@/components/scrollList';
import topBottomLayout from '@/components/topBottomLayout.vue';

import {mapActions, mapState} from 'vuex';
import {getPanelData} from '@/api/modules/SelectOrgApi';

import {
	ACTION_ADD_ENTITY,
	ACTION_REMOVE_ENTITY,
	ACTION_TOGGLE_ENTITY
} from '@/store/actionTypes';
import {EntityTypes, GroupId} from '@/utils/orgConstants';
import {BtnType, Dialog} from '@/components/dialog';
import {CheckUtils} from '@/utils/checkUtils';
import {getSelectKey, getTreeItems} from '@/utils/utils';
import {TreeUtils} from '@/utils/treeUtils';
import {MaskAsync} from '@/utils/Mask.js';

const NODATA_TAG = '_METADATA_';


export default {
	name: 'Department',
	components: {
		transferTree,
		scrollList,
		topBottomLayout
	},
	inject: ['app'],
	data() {
		return {
			treeData: [],
			accountIds: [],
			searchData: {},
			searchValue: '',
			defaultExpandedKeys: [],
			defaultSelected: [],
			radioList: [
				{ label: this.$i18n('org.member.label'), value: 'member' },
				{ label: this.$i18n('org.post.label'), value: 'post' },
				{ label: this.$i18n('org.role.label'), value: 'role' },
				{ label: '', value: 'memberAttribute' }
			],
			radioValue: 'member',
			// memberOtherProps: ['$_postName'],
			titleProps: { postName: this.$i18n('org.post.label'), deptName: this.$i18n('org.department.label') },
			postOtherProps: ['deptName'],
			roleOtherProps: [],
			selectValue: '',
			departmentId: '',
			// 判别是搜索人员或者部门下人员列表
			memberCommond: 'memberList',
			units: [],
			unitsString: '',
			//是否全集团搜索
			searchGroup: false,
			metadataList: [],
			currentSelectType: '',

			selectedAll: [],
			//首次加载时定位道当前部门
			isFirstLoadData: true
		};
	},
	watch: {
		selectedEntityMap: {
			deep: true,
			handler(val) {
				this.onSelectMap(val);
			}
		}
	},
	computed: {
		...mapState('selectData', ['selectedEntityMap']),
		checkedList() {
			return [ ...this.accountIds ];
		},
		showCheckboxInDeptTree() {
			return this.orgSelect.showCheckboxInDeptTree;
		},
		radioControl() {
			let radioContoller = {};
			for (let item of this.app().tabs) {
				if (item.name === 'Department') {
					radioContoller = item.extInfo;
				}
			}
			return radioContoller;
		},
		orgSelect() {
			return this.app().orgSelect;
		},
		selectType() {
			return this.orgSelect.selectType;
		},
		showGroupSearch() {
			return this.orgSelect.showGroupSearch && this.radioValue === 'member';
		},

		// 判断selectType是否只能搜索部门
		selectOnlyDep() {
			let {
				hiddenMemberOfDepartment,
				hiddenPostOfDepartment,
				hiddenRoleOfDepartment,
				hiddenMetadataOfDepartment
			} = this.radioControl;
			return (
				hiddenMemberOfDepartment
				&& hiddenPostOfDepartment
				&& hiddenRoleOfDepartment
				&& hiddenMetadataOfDepartment
			);
		},
		checkable() {
			// 是否显示checkbox
			// isShowCheckboxIntree?? 是否需要？？
			return this.orgSelect.selectType.includes(EntityTypes.Department);
			//	|| this.orgSelect.isShowCheckboxIntree;
		},
		placeText() {
			let radio = this.radioValue;
			let selectDept = this.orgSelect.selectType.includes(EntityTypes.Department);
			let selectMember = this.orgSelect.selectType.includes(EntityTypes.Member);
			let res = this.$i18n('selectPeople.input',`${this.$i18n('org.department.label')}/${this.$i18n('org.member.label')}`);
			if (this.selectOnlyDep) {
				res = this.$i18n('selectPeople.input',this.$i18n('org.department.label'));
			} else if (selectMember && !selectDept) {
				res = this.$i18n('selectPeople.input',this.$i18n('org.member.label'));
			} else if (radio === 'member') {
				res = this.$i18n('selectPeople.input',`${this.$i18n('org.department.label')}/${this.$i18n('org.member.label')}`);
			} else if (radio === 'post') {
				res = this.$i18n('selectPeople.input',this.$i18n('org.post.label'));
			} else if (radio === 'role') {
				res = this.$i18n('selectPeople.input',this.$i18n('org.role.label'));
			}
			return res;
		},
		// 当前所选单位id
		loginAccountId() {
			return this.app().orgSelect.loginAccountId;
		},
		/**
		 * showSecretLevel 为 true时添加密级展示
		*/
		memberOtherProps() {
			return this.onSetOtherProps(['$_postName'])
		},
    isShowCheckboxTree() {
      return this.app().isShowCheckboxTree;
    }
	},
	methods: {
		...mapActions('selectData', [ACTION_ADD_ENTITY, ACTION_REMOVE_ENTITY, ACTION_TOGGLE_ENTITY]),
		 listDataHandler(data) {
			// 集团搜索，岗位显示为【单位简称/岗位名称】
			const memberSearchFlag =  this.searchGroup && this.radioValue === 'member' && this.memberCommond === 'search';
			return data.map((item) => {
				const { type, name, accountShortName, postName, deptName, sourcePosts, accountId } = item;
				item.$_showLabel = name;
				if (memberSearchFlag) {
					// 集团搜索只有人员
					item.$_postName = `${accountShortName}/${deptName}`;
				} else {
					if (sourcePosts && sourcePosts.length) {
						let postList = [];
						sourcePosts.forEach((el) => {
							postList.push(el.name);
						});
						item.$_postName = postList.join(',');
					} else if (type === EntityTypes.Member) {
						item.$_postName = `${postName}`;
					} else if (type === EntityTypes.DepartmentPost && this.memberCommond === 'search' && this.currentSelectType === EntityTypes.Account) {
						// 岗位搜索且在单位更节点时
						item.$_showLabel = `${deptName}-${name}`;
					} else if (type === EntityTypes.Department && this.memberCommond === 'search') {
						let deptFullName = item.deptFullName;
						let names = deptFullName.split('/');
						if (names.length <= 1) {
							item.$_postName = `${deptFullName}`;
						} else {
							if (names.length > 2) {
								item.$_postName = `${names[names.length - 2]}`;
							} else {
								item.$_postName = `${item.accountShortName}`;
							}
						}
					}
					let currentAccountId = this.app().switchAfterUnit.id;
					if (type === EntityTypes.Member && accountId !== currentAccountId) {
						// 其他单位显示单位简称
						item.$_showLabel = `${name}(${accountShortName})`;
					}
				}
				item.$_selectKey = getSelectKey(item);
				return item;
			});
		},
		/**
		 * 加载树数据
		 * @param accountId 单位Id
		 * @returns {Promise<void>}
		 */
		async onGetTreeData(accountId) {
			// 标记单位
			this.$changeAccount(this, accountId);
			let {
				showChild,
				isNeedCheckLevelScope,
				onlyCurrentDepartment,
				showDepartmentsOfTree,
				showDepartmentsNoChildrenOfTree,
				accessFileSecretLevel
			} = this.orgSelect;
			let params = {
				panel: 'Department',
				command: 'departmentTree',
				arguments: {
					showChild,
					isNeedCheckLevelScope,
					onlyCurrentDepartment,
					showDepartmentsOfTree,
					showDepartmentsNoChildrenOfTree,
					accountId
				}
			};
			let res = await getPanelData(params);
			if (res.code !== 0) {
				this.$message.error(res.message);
				return;
			}
			let treeData = res.data;
			if (!treeData.length) {
				return;
			}
			// 树形checkBox显示和隐藏处理
			this.checkBoxHandler(treeData, accountId);
			// 树形复选框勾选处理
			this.onSelectMap(this.selectedEntityMap);
			// 处理树展开后、默认选择的节点
			const currentNodeType = this.dealDefaultData(treeData);
			// 赋值数据
			this.treeData = treeData;

			// 当前选择的数据类型
			this.currentSelectType = currentNodeType;

			this.departmentId = this.defaultSelected[0];

			/**
			 * 场景：加载单位部门树时判断是否初始化部门下的人员
			 * 2：selectOnlyDep tue|false 是否只可以选择部门, true只可以选择部门， false可以选择部门、人员
			 * 3: 只在radioValue为member
			 */
			if ( !this.selectOnlyDep && this.radioValue !== 'memberAttribute' ) {
				this.$nextTick(() =>{
					this.reloadListData({
						departmentId: this.departmentId,
						accessFileSecretLevel
					});
				});
			}
		},
		/**
		 * 切换选择类型
		 * @param e
		 */
		onRadioChange(e) {
			let radio = e.target.value;
			this.memberCommond = 'memberList';
			if (radio === 'member') {
				let {
					showChild,
					isNeedCheckLevelScope,
					queryOrgLevel,
					showConcurrentMember,
					accessFileSecretLevel
				} = this.orgSelect;
				this.$nextTick(() =>{
					this.reloadListData({
						departmentId: this.departmentId,
						searchGroup: this.searchGroup,
						showChild,
						isNeedCheckLevelScope,
						queryOrgLevel,
						showConcurrentMember,
						accessFileSecretLevel
					});
				});
			} else if (radio === 'post' || radio === 'role') {
				this.$nextTick(() =>{
					this.reloadListData({
						departmentId: this.departmentId
					});
				});
			} else if (radio === 'memberAttribute') {
				this.onMetaChange(this.selectValue);
			}
		},
		/**
		 * 选择树节点
		 * @param data
		 */
		onSelectNode(data) {
			let {
				showChild,
				isNeedCheckLevelScope,
				queryOrgLevel,
				showConcurrentMember,
				accessFileSecretLevel
			} = this.orgSelect;
			if (!data.selected.length) {
				return;
			}
			this.departmentId = data.id;

			let type = data.selected[0].type;
			// 当前选择的数据类型
			this.currentSelectType = type;
			if (this.selectOnlyDep) {
				return;
			}
			if (this.radioValue === 'memberAttribute') {
				this.onMetaChange(this.selectValue);
			} else {
				this.memberCommond = 'memberList';
				this.$nextTick(() =>{
					this.reloadListData({
						departmentId: this.departmentId,
						searchGroup: this.searchGroup,
						showChild,
						isNeedCheckLevelScope,
						queryOrgLevel,
						showConcurrentMember,
						accessFileSecretLevel
					});
				});
			}
		},
		/**
		 * 主数据选择改变
		 * @param val
		 */
		onMetaChange(val) {
			if (val === NODATA_TAG) {
				this.$refs.scrollList.initProps();
				return;
			}
			let data = this.getMetaDataId(val);
			this.$nextTick(() =>{
				this.reloadListData({
					departmentId: this.departmentId,
					...data
				});
			});
		},
		/**
		 * 处理组织属性的id
		 */
		getMetaDataId(val) {
			let data = {};
			if ( !this.radioControl.hiddenMetadataOfDepartment && this.metadataList.length ) {
				let index = this.metadataList.findIndex((el) => el.id === val);
				if (index !== -1) {
					data = {
						enumId: this.metadataList[index].enumId,
						metadataId: this.metadataList[index].id
					};
				}
			}
			return data;
		},
		onSearch(val) {
			// selectOnlyDep = true 只支持搜索部门，selectOnlyDep = false 支持搜索SeletType中的Member,Post,Role
			if (this.selectOnlyDep) {
				if (val) {
					this.$refs.topBottomLayout.showAll();
					this.memberCommond = 'search';
					let { isNeedCheckLevelScope } = this.app().orgSelect;
					let accountId = this.app().switchAfterUnit.id;
					this.$nextTick(() =>{
						this.reloadListData({
							keyword: this.searchValue,
							searchType: 'Department',
							departmentId: this.departmentId,
							// currentDeptId: this.departmentId,
							unitId: accountId,
							showChild: this.orgSelect.showChild,
							isNeedCheckLevelScope
						});
					});
				} else {
					this.$refs.topBottomLayout.showOnlyTop();
				}
			} else {
				this.$refs.topBottomLayout.showAll();
				this.onSearchType(val);
			}
		},
		reloadListData(data) {
			if (this.$refs.scrollList) {
				this.$refs.scrollList.search(data);
			}
		},
		onChangeAccount(data) {
			if (data.id === GroupId) {
				// 集团根节点不显示
				return;
			}
			// 切换单位后有部门页签
			this.searchValue = '';
			this.radioValue = 'member';
			this.memberCommond = 'memberList';
			this.onGetTreeData( data.id);
		},
		/**
		 * 展开树的默认节点
		 * @param tree
		 */
		dealDefaultData(tree) {
			let accountNode = tree[0];
			// 当前单位Id
			let accountId = accountNode.id;
			// 默认单位
			let defaultAccountId = this.orgSelect.defaultAccountId;
			// 默认部门【可能为空】
			let defaultDepartmentId = this.orgSelect.defaultDepartmentId;
			// 默认展开的节点
			let defaultExpandedKeys = [];
			let defaultSelectedKey = '';
			let currentNodeType = EntityTypes.Department;

			let hasDefault = false;
			if (accountId === defaultAccountId && defaultDepartmentId) {
				// 查找部门对应的parent
				let result = TreeUtils.findParents(tree,defaultDepartmentId);
				if (result.success) {
					defaultExpandedKeys = result.nodeIds;
					defaultSelectedKey = defaultDepartmentId;
					hasDefault = true;
				}
			}
			// 没有选择默认的，默认展开单位一级
			if (!hasDefault) {
				defaultExpandedKeys = [accountNode.id];
			}
			if (defaultSelectedKey === '') {
				if (accountNode.children && accountNode.children.length) {
					// 默认选择单位下的第一个部门
					defaultSelectedKey = accountNode.children[0].id;
				} else {
					// 只能选择单位节点了
					defaultSelectedKey = accountNode.id;
					currentNodeType = EntityTypes.Account;
				}
			}
			// 默认展开
			this.defaultExpandedKeys = defaultExpandedKeys;
			// 默认选择
			this.defaultSelected = [defaultSelectedKey];
			console.log('this.defaultSelected',this.defaultSelected);
			return currentNodeType;
		},
		onSearchType(val) {
			const {
				showConcurrentMember,
				queryOrgLevel,
				showChild,
				isNeedCheckLevelScope,
				secretLevelManager,
				accessFileSecretLevel,
				selectType
			} = this.orgSelect;

			if (this.radioValue === 'member') {
				let selectDept = selectType.includes(EntityTypes.Department);
				let selectMember = selectType.includes(EntityTypes.Member);
				let searchType = '';
				if (selectDept && selectMember) {
					searchType = 'Department,Member';
				} else if (this.selectOnlyDep) {
					searchType = 'Department';
				} else {
					searchType = 'Member';
				}
				if (val) {
					// 搜索部门/人员
					this.memberCommond = 'search';
					let accountId = this.app().switchAfterUnit.id;
					this.$nextTick(() =>{
						this.reloadListData({
							keyword: val,
							searchGroup: this.searchGroup,
							unitId: accountId,
							searchType: searchType,
							showChild,
							accessFileSecretLevel,
							isNeedCheckLevelScope
						});
					});
				} else {
					// 加载部门对应人员
					this.memberCommond = 'memberList';
					this.$nextTick(() =>{
						this.reloadListData({
							searchGroup: this.searchGroup,
							departmentId: this.departmentId,
							searchType: 'Member',
							showConcurrentMember,
							queryOrgLevel,
							isNeedCheckLevelScope,
							secretLevelManager,
							accessFileSecretLevel
						});
					});
				}
			} else {
				let metaDataIdObj = this.getMetaDataId(this.selectValue);
				this.reloadListData({
					keyword: val,
					departmentId: this.departmentId,
					searchGroup: this.searchGroup,
					...metaDataIdObj
				});
			}
		},
		/**
		 * 初始化radioValue值
		 */
		onInitRadioValue() {
			if (this.selectType.includes(EntityTypes.Member)) {
				this.radioValue = 'member';
			} else if (!this.radioControl.hiddenPostOfDepartment) {
				this.radioValue = 'post';
			} else if (!this.radioControl.hiddenRoleOfDepartment) {
				this.radioValue = 'role';
			} else if (!this.radioControl.hiddenMetadataOfDepartment && this.metadataList.length > 0) {
				this.radioValue = 'memberAttribute';
			}
		},
		onSelectMap(val) {
			let pathKeys = Object.keys(val);
			if (pathKeys.length) {
				this.onTreeNodeAccount(val);
			} else {
				this.accountIds = [];
			}
		},
		checkBoxHandler(node, accountId) {
			const { includeElements, showCheckboxInDeptTree, selectType } = this.orgSelect;
			if (!includeElements || includeElements === '') {
				if(!showCheckboxInDeptTree && node[0].type === EntityTypes.Account && !selectType.includes(EntityTypes.Account)){
					node[0].disabled = true;
				}
				return;
			}
			if (includeElements.indexOf(EntityTypes.Account + '|' + accountId) !== -1
				|| includeElements.indexOf(EntityTypes.Account + '|' + GroupId) !== -1) {
				return;
			}
			this.handleCheckBox(node[0]);
		},
		/**
		 * 递归节点隐藏checkBox
		 * @param node 当前节点的数据
		 * @param accountId 单位Id
		 * @param parentPath 父节点path
		 */
		handleCheckBox(node, parentPath = '') {
			if (!node) {
				return;
			}
			let { includeElements } = this.orgSelect;
			let nodePath = '';
			if (includeElements.indexOf(EntityTypes.Department + '|' + node.id) !== -1) {
				node.disabled = false;
				nodePath = node.path;
			} else {
				node.disabled = true;
			}
			if (parentPath && node.path.startsWith(parentPath)) {
				node.disabled = false;
				nodePath = node.path;
			}
			if (node.children && node.children.length !== 0) {
				for (let item of node.children) {
					this.handleCheckBox(item, nodePath);
				}
			}
		},
		/**
		 * 节点被选中
		 * @param nodeData 当前节点的数据
		 * @param nodeEl 被选择的元素
		 * @returns {Promise<void>}
		 */
		// @MaskAsync
		async onCheckedDeptNode(nodeData, nodeEl) {
			let { showCheckboxInDeptTree, isConfirmExcludeSubDepartment } = this.orgSelect;
      console.log(showCheckboxInDeptTree, isConfirmExcludeSubDepartment, this.orgSelect)
			// 当前节点是否被选中
			let currentNodeState = nodeEl.checked;
			let currentNode = nodeData.checkeds[0];
			let nodeType = currentNode.type;

      let treeItems = this.isShowCheckboxTree ? getTreeItems(currentNode) : [currentNode]
			// 取消选择
			if (currentNodeState) {
				this[ACTION_REMOVE_ENTITY]({
					entities:  [currentNode]
				});
				return;
			}


			let notIncludeSubDept = [];
			let checkContext = {
				currentPanel: 'Department',
				notIncludeSubDept
			};
      if( this.isShowCheckboxTree) {

        // 提示是否排查子部门
        let btnType = BtnType.OK;
        if (currentNode.children && currentNode.children.length && nodeType === 'Department') {
          btnType = await Dialog.confirm({
            msg: `"${currentNode.name}"${this.$i18n('selectPeople.ConfirmChildDept')}`,
            okText: this.$i18n('common.include.label'),
            cancelText: this.$i18n('common.not.include.label')
          });
        }
        if (btnType === BtnType.CLOSE) {
          // 关闭
          return;
        }
        if (btnType === BtnType.CANCEL) {
          // 不包含
          // notIncludeSubDept.push(currentNode.id);
          treeItems = [currentNode]
        }
        let result = await CheckUtils.doCheckEntity(currentNode, checkContext);
        if (!result) {
          return;
        }
        this[ACTION_ADD_ENTITY]({
          entities: treeItems,
          notIncludeSubDept
        });

      } else {
        // 老版本checkbox方式显示，只是选中当前效果
        if (showCheckboxInDeptTree) {
          // 选多部门时-选择单位时勾选子部门
          if (nodeType === EntityTypes.Account && !this.selectType.includes(EntityTypes.Account)) {
            let data = TreeUtils.findDeptChildren(this.treeData, this.selectedEntityMap,false);
            let entities = [];
            for (let entity of data) {
              let result = await CheckUtils.doCheckEntity(entity, checkContext);
              if (!result) {
                continue;
              }
              entities.push(entity);
            }
            if (entities.length) {
              this[ACTION_ADD_ENTITY]({ entities: entities });
            }
            return;
          }

          // 单独选择
          let result = await CheckUtils.doCheckEntity(currentNode, checkContext);
          if (!result) {
            return;
          }
          // 老版本的树显示checkbox,选择什么就是什么
          this[ACTION_ADD_ENTITY]({
            entities: [currentNode]
          });
          return;
        }


        // 提示是否排查子部门
        let btnType = BtnType.OK;
        if (isConfirmExcludeSubDepartment && currentNode.children && currentNode.children.length && nodeType === 'Department') {
          btnType = await Dialog.confirm({
            msg: `"${currentNode.name}"${this.$i18n('selectPeople.ConfirmChildDept')}`,
            okText: this.$i18n('common.include.label'),
            cancelText: this.$i18n('common.not.include.label')
          });
        }
        if (btnType === BtnType.CLOSE) {
          // 关闭
          return;
        }
        if (btnType === BtnType.CANCEL) {
          // 不包含
          notIncludeSubDept.push(currentNode.id);
          treeItems = [currentNode]
        }
        let result = await CheckUtils.doCheckEntity(currentNode, checkContext);
        if (!result) {
          return;
        }

        this[ACTION_TOGGLE_ENTITY]({
          entities: treeItems,
          notIncludeSubDept
        });
      }
		},
		/**
		 * 部门树勾选
		 */
		onTreeNodeAccount(data) {
			this.accountIds = [];
			let typeIds = Object.keys(data);
			for (let item of typeIds) {
				if (item.indexOf('Account') !== -1
					|| item.indexOf('Department') !== -1 ) {
					let deptId = item.split('|')[1];
					this.accountIds.push(deptId);
				}
			}
		},
		/**
		 * 选择部门时的处理逻辑
		*/
		async onChooseDeptNode(selectNode) {
			let { isConfirmExcludeSubDepartment } = this.app().orgSelect;
			let currentNode = this.onFindDeptNode(selectNode, this.treeData);
			let nodeType = currentNode.type;
			let notIncludeSubDept = [];
			let checkContext = {
				currentPanel: 'Department',
				notIncludeSubDept
			};
			// 提示是否排查子部门
			let btnType = BtnType.OK;
			if (isConfirmExcludeSubDepartment && currentNode.children && currentNode.children.length && nodeType === 'Department') {
				btnType = await Dialog.confirm({
					msg: `"${currentNode.name}"${this.$i18n('selectPeople.ConfirmChildDept')}`,
					okText: this.$i18n('common.include.label'),
					cancelText: this.$i18n('common.not.include.label')
				});
			}
			if (btnType === BtnType.CLOSE) {
				// 关闭
				return;
			}
			if (btnType === BtnType.CANCEL) {
				// 不包含
				notIncludeSubDept.push(currentNode.id);
			}
			let result = await CheckUtils.doCheckEntity(currentNode, checkContext);
			if (!result) {
				return;
			}
			this[ACTION_TOGGLE_ENTITY]({
				entities: [currentNode],
				notIncludeSubDept
			});
		},
		/**
		 * 查找部门节点
		*/
		onFindDeptNode(selectNode, treeData) {
			let currentNode = {...selectNode};
			const deptNodeFunc = (selectEntity, data) => {
				let nodeId = selectEntity.id;
				for (let node of data) {
					if (node.id === nodeId) {
						currentNode.children = node.children;
						break;
					}
					if (node.children && node.children.length > 0) {
						deptNodeFunc(selectEntity, node.children);
					}
				}
			};
			deptNodeFunc(selectNode, treeData);
			return currentNode;
		},
		/**
		 * 滚动树到可视区域
		 * @param id
		 */
		onScrollIntoSelectedNode(id) {
			let nodeElement = document.getElementById(id);
			if (nodeElement && this.isFirstLoadData) {
				nodeElement.scrollIntoView({behavior: 'smooth', block: 'center', inline: 'nearest'});
				this.isFirstLoadData = false;
			}
		},
		/**
		 * 人员【密级】展示处理
		 * @param {Array} list 默认展示的人员信息
		*/
		onSetOtherProps(list) {
			if (this.orgSelect.showSecretLevel) {
				list.unshift('secretLevel')
			}
			return list
		}
	},
	created() {
		if ( this.radioControl.metadataList && this.radioControl.metadataList.length ) {
			this.metadataList = [
				{ id: NODATA_TAG, name: `-${this.$i18n('org.member.attributes.lable')}-` },
				...this.radioControl.metadataList
			];
			this.selectValue = NODATA_TAG;
		}
	},
	activated() {
		const { id: accountId } = this.app().switchAfterUnit;
		if (this.$isChangeAccount(this, accountId)) {
			this.onInitRadioValue();
			this.onGetTreeData(accountId);
		}
	}
};
</script>

<style scoped lang="scss">

.small-input-wraper{
	width: calc(100% - 104px);
}
.large-input-wraper {
	width: 100%;
	& .ant-input-search-button {
		border-right: 0;
	}
}
.ant-input {
	border-radius: 0;
	height: 32px;
	box-sizing: border-box;
}

.small-select-option {
	&/deep/ .ant-select-selection{
		overflow: hidden;
		width: 100px;
		border: none;
		border-color: #f5f5f5;
		background: #f5f5f5;
		box-shadow: none;
		.ant-select-arrow{
			font-size: 12px;
			color: rgba(0,0,0,0.25);
		}
	}
}

   .checkbox-wrapper {
	height: 32px;
	line-height: 32px;
	padding-left: 10px;
	border-left: 1px solid $borderColor;
	width: 104px;
	display: inline-block;
	float: right;

	.label-text{
		color: $postDefalutFontColor;
	}

	& /deep/ >span::after,& /deep/ >span:after{
		content: " ";
		display: block;
		clear: both;
	}

}

</style>
